//
//  MagicMoveAnimate.swift
//  lookLikeIweekly
//
//  Created by ZhangXiaoqian on 16/6/13.
//  Copyright © 2016年 Mainzxq. All rights reserved.
//

import UIKit

class MagicMoveAnimate: NSObject,UIViewControllerAnimatedTransitioning {
    
    let duration = 0.5
    
    // 指定转场动画持续的时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return duration
    }
    
    // 实现转场动画的具体内容
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        // 得到容器视图
        let containerView = transitionContext.containerView()
        // 目标视图
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        containerView!.addSubview(toView)
        containerView!.addSubview(fromView)        // 为目标视图的展现添加动画
        let frameWidth = containerView?.bounds.size.width
        let frameHeight = containerView?.bounds.size.height
        toView.alpha = 0.5
        toView.frame = CGRectMake(frameWidth!, 0, frameWidth!, frameHeight!)
        UIView.animateWithDuration(duration,
                                   animations: {
                                    toView.alpha = 1.0
                                    toView.frame = CGRectMake(0, 0, frameWidth!, frameHeight!)
                                    fromView.frame = CGRectOffset(fromView.frame, -frameWidth!, 0)
            }, completion: { _ in
                transitionContext.completeTransition(true)
        })
    }
    
    
}
